import { ifs, ts, Tsrv } from '@/services';

class T_cmd {
  // 主组件箱
  private compBox: ifs.I_compBox;

  // 主组件ID
  private compID: string;

  // 主组件实例
  private self: any;
  // 主组refs

  // 主组件created里初始化
  public init(self: any, compBoxInfo: ifs.I_compBoxInfo) {
    this.self = self;
    this.compBox = compBoxInfo.TcompBox;
    this.compID = compBoxInfo.compID;
  }

  public getTcmd(): T_cmd {
    return this;
  }

  public doLoadData(tableName: string, filter = {}) {
    const gpd = new ts.T_getPartDataArg();
    gpd.setFilter(filter);
    gpd.setFunName(tableName);
    gpd.setTableName(tableName);
    gpd.setDbType(Tsrv.globalVar.dbType.MYSQL);
    gpd.setLang(this.self.$i18n.locale);
    gpd.setPage(1, 99999);
    gpd.setRule({} as ifs.I_rule);

    return Tsrv.getData(gpd);
  }

  public loadFilterList(
    field: string[],
    funName: string,
    filter = {}
  ): Promise<
    Array<{
      [key: string]: any;
    }>
  > {
    const gpfd: ts.T_GPFD = new ts.T_GPFD();
    gpfd
      .setLang(this.self.$i18n.locale || 'en-US')
      .setFunName(funName)
      .setTableName(funName)
      .setDbType(Tsrv.globalVar.dbType.MYSQL);
    gpfd.setFilter(filter).setAgreedFields(...field);

    return new Promise((resolve, reject) => {
      Tsrv.getData<any, ifs.I_GPFD>(gpfd)
        .then(r => {
          const [b, res, rows, msg] = r;
          resolve(rows);
        })
        .catch(err => {
          this.self.$msg(err);
        });
    });
  }
}
interface I_cmd extends T_cmd {}
export { I_cmd, T_cmd };
